package com.shketai.action;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.shketai.entity.Imessage;
import com.shketai.entity.PageBean;
import com.shketai.service.ImessageService;

public class ImessageAction extends ActionSupport implements ModelDriven<Imessage>{

	private static final long serialVersionUID = 1L;
	
	private int result;  
	
	private int user_id;
	
	private int teacher_id;
	
	private String user_ids;
	
	private int id;
	
	private PageBean<Imessage> pb;
	
	private int page;    //页码
	
	private Imessage imessage = new Imessage();
	
	private ImessageService imessageService;
	
	private List<Imessage> Imessages;
	
	
	public int getResult() {
		return result;
	}


	public void setResult(int result) {
		this.result = result;
	}


	public Imessage getImessage() {
		return imessage;
	}


	public void setImessage(Imessage imessage) {
		this.imessage = imessage;
	}


	public void setImessageService(ImessageService imessageService) {
		this.imessageService = imessageService;
	}

	public int getUser_id() {
		return user_id;
	}


	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<Imessage> getImessages() {
		return Imessages;
	}


	public void setImessages(List<Imessage> imessages) {
		Imessages = imessages;
	}


	public String getUser_ids() {
		return user_ids;
	}


	public void setUser_ids(String user_ids) {
		this.user_ids = user_ids;
	}


	public PageBean<Imessage> getPb() {
		return pb;
	}


	public void setPb(PageBean<Imessage> pb) {
		this.pb = pb;
	}


	public int getPage() {
		return page;
	}


	public void setPage(int page) {
		this.page = page;
	}


	public int getTeacher_id() {
		return teacher_id;
	}


	public void setTeacher_id(int teacher_id) {
		this.teacher_id = teacher_id;
	}


	/**
	 * 增加一条站内信
	 * @return
	 */
	public String add(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		if(session.get("id") == null){
			result = 0;//未登陆，不能发送站内信
		}else{
			int tid = Integer.parseInt(session.get("id").toString());
			imessageService.add(imessage.getHead(), imessage.getMessage(),  tid);;
			result = 1;
		}
		return SUCCESS;
	}
	
	/**
	 * 学生端查看站内信
	 * @return
	 */
	public String find(){
		pb = imessageService.find(user_id, page);
		return SUCCESS;
	}
	
	/**
	 * 标记已读
	 * @return
	 */
	public String updateFlag(){
		imessageService.updateFlag(imessage.getId());
		result = 1;
		return SUCCESS;
	}
	
	/**
	 * 查看详情
	 */
	@Override
	public String execute() throws Exception {
		imessage = imessageService.findById(imessage.getId());
		return SUCCESS;
	}
	
	/**
	 * 删除消息
	 * @return
	 */
	public String delete(){
		imessageService.delete(imessage);
		result = 1;
		return SUCCESS;
	}
	
	/**
	 * 未读数
	 * @return
	 */
	public String count(){
		result = imessageService.count(user_id);
		return SUCCESS;
	}


	@Override
	public Imessage getModel() {
		// TODO Auto-generated method stub
		return imessage;
	}
}
